//
//  DiscoverTableViewCellOne.m
//  XYFMApp
//
//  Created by 岁变 on 16/8/31.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "DiscoverTableViewCellOne.h"
#import "XYMacro.h"

@implementation DiscoverTableViewCellOne

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"find_cat_header"]];
        [self.contentView addSubview:_backImageView];
        
        _typeLabel = [[UILabel alloc] init];
        _typeLabel.textColor = [UIColor whiteColor];
        _typeLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_typeLabel];
        
        _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_moreButton setTitle:@"更多" forState:UIControlStateNormal];
        [_moreButton setTitleColor:[UIColor colorWithRed:0.46 green:0.47 blue:0.50 alpha:1.00] forState:UIControlStateNormal];
        _moreButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [_moreButton addTarget:self action:@selector(nextPage:) forControlEvents:UIControlEventTouchUpInside];

        [self.contentView addSubview:_moreButton];
        
        _lineView = [[UIView alloc] initWithFrame:CGRectZero];
        _lineView.backgroundColor = [UIColor colorWithRed:1.00 green:0.27 blue:0.12 alpha:1.00];
        [self.contentView addSubview:_lineView];
        
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor colorWithRed:0.08 green:0.09 blue:0.12 alpha:1.00];
        [self.contentView addSubview:_collectionView];
        [_collectionView registerClass:[DiscoverCollectionViewCellOfContentViewThird class] forCellWithReuseIdentifier:@"DiscoverCollectionViewCellOfContentViewThird"];
        
        
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _model.musics.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DiscoverCollectionViewCellOfContentViewThird *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverCollectionViewCellOfContentViewThird" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:0.08 green:0.09 blue:0.12 alpha:1.00];
    cell.model = [_model.musics objectAtIndex:indexPath.row];
    return cell;
}


- (void)setModel:(DiscoverModelFirstMain *)model {
    _model = [[DiscoverModelFirstMain alloc] init];
    _model = model;
    _typeLabel.text = model.channel.channel_name;
    [_collectionView reloadData];
}


- (void)nextPage:(UIButton *)button {
   VCOfDiscover *discoverVC = (VCOfDiscover *)self.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder;
    VCOfClassify *nextVC = [[VCOfClassify alloc] init];
    nextVC.channel_id = [NSString stringWithFormat:@"%ld", _model.channel.channel_id];
    [discoverVC.navigationController pushViewController:nextVC animated:YES];
    
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    VCOfDiscover *vc = (VCOfDiscover *)self.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder.nextResponder;
    VCOfDiscoverList *nextVC = [[VCOfDiscoverList alloc] init];
    DiscoverModelFirstTypeContenModel *model = [_model.musics objectAtIndex:indexPath.row];
    nextVC.item_id = [NSString stringWithFormat:@"%ld", model.item_id];
    [vc.navigationController pushViewController:nextVC animated:YES];
}





- (void)layoutSubviews {
    [super layoutSubviews];
    
    [_backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(5);
        make.left.equalTo(self.contentView.mas_left).with.offset(5);
        make.width.mas_offset(30);
        make.height.mas_offset(30);
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(34);
        make.left.equalTo(self.contentView.mas_left).with.offset(15);
        make.right.equalTo(self.contentView.mas_right).with.offset(-15);
        make.height.mas_offset(1);
    }];
    
    [_typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(5);
        make.left.equalTo(_backImageView.mas_right).with.offset(5);
        make.width.mas_offset(200);
        make.height.mas_offset(28);
        
    }];
    
    [_moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(5);
        make.right.equalTo(self.contentView.mas_right).with.offset(-15);
        make.height.mas_offset(28);
        make.width.mas_offset(30);
    }];
    
    _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 75) / 4, 170 - 50);
    _layout.minimumLineSpacing = 15;
    _layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(40);
        make.left.equalTo(self.contentView.mas_left).with.offset(0);
        make.right.equalTo(self.contentView.mas_right).with.offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-10);
        
    }];
}



@end
